﻿#if UNITY_EDITOR
using System.Collections.Generic;
using System.Text.RegularExpressions;
/// <summary>
/// 编译工具
/// </summary>
public static class ESF_CSharpCompilerTool
{
    /// <summary>
    /// 正则代码规则
    /// </summary>
    const string mRegexCodeRule = @"(<%[\s|\S]+?%>)";

    /// <summary>
    /// 分解写入块
    /// </summary>
    /// <param name="_codeTemplete">代码模板</param>
    /// <returns>写入块</returns>
    public static List<AbsESF_WriteBlock> ResolveWriteBlock(string _codeTemplete)
    {
        List<AbsESF_WriteBlock> blocks = new List<AbsESF_WriteBlock>();
        MatchCollection matchs = Regex.Matches(_codeTemplete, mRegexCodeRule);
        if (matchs != null && matchs.Count > 0)
        {
            AbsESF_WriteBlock cwb = null;
            int startIndex = 0;
            int length = 0;
            foreach (Match key in matchs)
            {
                length = key.Index - startIndex;
                if (length > 0)
                {//每个<%%>的前面都是文本块
                    cwb = new ESF_CSharpWriteTextBlock(_codeTemplete.Substring(startIndex, length));
                    blocks.Add(cwb);
                }
                cwb = new ESF_CSharpWriteCodeBlock(key.Value);
                blocks.Add(cwb);
                startIndex = key.Index + key.Length;
            }

            if (startIndex < _codeTemplete.Length)
            {//最后一个<%%>的后面也是文本块
                cwb = new ESF_CSharpWriteTextBlock(_codeTemplete.Substring(startIndex, _codeTemplete.Length - startIndex));
                blocks.Add(cwb);
            }
        }
        else
        {
            blocks.Add(new ESF_CSharpWriteTextBlock(_codeTemplete));
        }
        return blocks;
    }
}
#endif
